// ChatClientThiCuoiKy.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <WinSock2.h>
#include <stdio.h>
#include <iostream>
using namespace std;

const int bufferSize = 1055;

typedef struct chatTransfer{
	int iType;
	int iLength;
	char szNickname[32];
	char bData[1024];
};

void ChatTransferToBuffer(struct chatTransfer *chat, char* buf)
{
	for (int i = 0; i < bufferSize; i++) buf[i] = 0; // clear buffer
	buf[0] = chat->iType;
	buf[1] = chat->iLength;
	for (int i = 0; i < 32; i++) buf[i + 2] = chat->szNickname[i];
	for (int i = 0; i < 1024; i++) buf[i + 34] = chat->bData[i];
}
DWORD WINAPI ReceiverThread(LPVOID IpPatameter);
int _tmain(int argc, _TCHAR* argv[])
{
	struct chatTransfer *chatSend = new chatTransfer(), *chatRecv;
	char* bufSend[bufferSize], bufReceiver[bufferSize], username[32], friendname[32];
	// khoi tao gui
	WSADATA wsaData;
	SOCKET s;
	SOCKADDR_IN ServerAddr;
	int Port = 8888;
	char* server = "127.0.0.1";
	WSAStartup(MAKEWORD(2, 2), &wsaData);
	s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	ServerAddr.sin_family = AF_INET;
	ServerAddr.sin_port = htons(Port);
	ServerAddr.sin_addr.S_un.S_addr = inet_addr(server);
	connect(s, (SOCKADDR*)&ServerAddr, sizeof(ServerAddr));
	// tao luong nhan du lieu
	CreateThread(0, 0, ReceiverThread, 0, 0, 0);

	// login
	cout << "Nhap nickname cua ban: ";
	cin >> username;
	cout << "Connecting..." << endl;

	// send login struct
	chatSend->iType = 0;
	chatSend->iLength = 0;
	/*for (int i = 0; i < 32; i++) {
		 *chatSend->szNickname = username[i];
	}
	*/
	strcpy(chatSend->szNickname, username);
	ZeroMemory(chatSend->bData, 1024);
	ChatTransferToBuffer(chatSend, *bufSend);

	return 0;
}


DWORD WINAPI ReceiverThread(LPVOID IpPatameter)
{

	return NULL;
}